<?php
namespace Model\Admin;
use Spartan\Lib\Model;

class Users extends Model
{
    /**
     * 系统用户登录
     * @return array
     */
    public function login(){
        $arrData = Array(
            'user_name'=>Array('required','length',[2,32],'请输入正确的用户名。'),
            'pass_word'=>Array('required','length',[6,32],'请输入正确的密码。'),
            'ip'=>'$$ip',
            'add_time'=>'$$add_time',
        );
        list($arrData,$arrResult) = $this->valid($arrData)->result();
        if ($arrResult[1] != 1){
            return $arrResult;
        }
        $options = array(
            'alias'=>'a',
            'field' =>'a.real_name,a.user_name,a.id,email,face,b.menu_list,'.
                'b.name AS group_name,b.power_list,a.user_menu,a.user_power,a.status,b.id AS group_id,'.
                'login_time as per_login_time,login_ip as per_login_ip',
            'join' =>Array(
                'INNER JOIN @.admin_user_group AS b ON a.group_id = b.id'
            ),
            'where' =>Array(
                "user_name" => $arrData['user_name'],
                'pass_word' => md5("{$arrData['user_name']}={$arrData['pass_word']}")
            ),
        );
        $rsInfo = db()->find('admin_users',$options);
        if (!$rsInfo){
            $this->getModelClass('admin_login_log')->setData($arrData)->saveLog();
            return Array('用户名和密码错误。',0,[]);
        }
        if ($rsInfo['status'] != 1 ){
            $arrData['pass_word'] = '锁定后尝试登录。';
            $this->getModelClass('admin_login_log')->setData($arrData)->saveLog();
            return Array('用户已经被锁定，无法登录。',0,[]);
        }
        if (is_numeric($arrData['pass_word']) && mb_strlen($arrData['pass_word'],'utf-8') <= 8){
            return Array('密码简单，要求更改。',2,Array('id'=>$rsInfo['id'],'user_name'=>$rsInfo['user_name']));
        }
        $rsInfo['user_menu'] && $rsInfo['menu_list'] .= ',' . $rsInfo['user_menu'];
        $rsInfo['user_power'] && $rsInfo['power_list'] .= '@' . $rsInfo['user_power'];
        $temp = explode(',',$rsInfo['menu_list']);
        array_unique($temp);$rsInfo['menu_list'] = implode(',',$temp);
        $temp = explode(',',$rsInfo['power_list']);
        array_unique($temp);$rsInfo['power_list'] = implode(',',$temp);
        unset($rsInfo['user_menu']);
        unset($rsInfo['user_power']);
        $rsInfo['login_time'] = $arrData['add_time'];
        $rsInfo['login_ip'] = $arrData['ip'];
        substr($rsInfo['power_list'],0,3)=='all' && $rsInfo['power_list'] = 'all';
        substr($rsInfo['power_list'],0,4)=='view' && $rsInfo['power_list'] = 'view';
        //**修改登录信息//
        $arrDataLog = Array(
            'login_ip'=>$arrData['ip'],
            'login_time'=>$arrData['add_time'],
            'login_count'=>Array('exp','login_count+1'),
        );
        $options = Array(
            'where'=>Array('id'=>$rsInfo['id']),
        );
        $result = db()->update('admin_users',$arrDataLog,$options);
        if ($result == false){
            $arrDataLog['level'] = 1;
            $arrDataLog['info'] = '更新管理员登录信息错误';
            $this->sysError($arrDataLog);
        }
        //** 加入成功的记录 */
        $arrData['status'] = 1;
        $this->getModelClass('admin_login_log')->setData($arrData)->saveLog();
        return Array('登陆成功',1,$rsInfo);
    }

    /**
     * 系统用户注册
     * @return array
     */
    public function register(){
        $arrData = Array(
            'user_name'=>Array('required','length',[2,32],'请输入正确的登录用户名。'),
            'real_name'=>Array('required','chinese',[2,32],'请输入正确的真实姓名(中文)。'),
            'email'=>Array('required','email&length',[5,80],'请输入正确的电子邮箱。'),
            'pass_word'=>Array('required','length',[6,32],'请输入正确的密码。'),
            'group_id'=>['null','num',[1],'请选择权限组。'],
            'status'=>['null','num',[1],'请选择权限组。'],
            'login_ip'=>'$$ip',
            'add_time'=>'$$add_time',
        );
        list($arrData,$arrResult) = $this->valid($arrData)->result();
        if ($arrResult[1] != 1){
            return $arrResult;
        }
        $arrData['pass_word'] = md5("{$arrData['user_name']}={$arrData['pass_word']}");
        $arrData['status'] = 0;
        $options = Array(
            'where'=>Array(
                'user_name'=>$arrData['user_name'],
            )
        );
        $arrInfo = db()->find('admin_users',$options);
        if ($arrInfo){
            return Array('管理用户名已经存在，请更换。',0);
        }
        //这里可以设置默认开通的组
        //$arrData['group_id'] = 1;
        //$arrData['status'] = 1;
        $result = db()->insert('admin_users',$arrData);
        if (!$result){
            return Array('管理用户插入失败，请联系管理员。',0);
        }
        return Array('提交注册成功，请稍等超级管理员审核并开通。',1);
    }

    /**
     * 验证用户找回密码
     */
    public function lostPass(){
        $arrData = Array(
            'user_name'=>Array('required','length',[2,32],'请输入正确的用户名。'),
            'email'=>Array('required','email&length',[6,32],'请输入正确的电子邮箱。'),
            'login_ip'=>'$$ip',
            'add_time'=>'$$add_time',
        );
        list($arrData,$arrResult) = $this->valid($arrData)->result();
        if ($arrResult[1] != 1){
            return $arrResult;
        }
        $options = Array(
            'where'=>Array(
                'user_name'=>$arrData['user_name'],
                'email'=>$arrData['email'],
            )
        );
        $arrInfo = db()->find('admin_users',$options);
        if (!$arrInfo){
            return Array('登录用户名对应的电子邮件不存在，请确认。',0);
        }

    }

    /**
     * 保存找回的密码
     */
    public function saveLostPass(){

    }
    /**
     * 修改用户的初始密码
     */
    public function edit(){
        $arrData = Array(
            'id' => array('required','num',[2,32],'登录超时，请重新登录！'),
            'user_name'=>Array('required','length',[2,32],'登录超时，请重新登录！'),
            'pass_word'=>Array('required','length',[6,32],'请输入正确的密码[%s-%s位]！'),
            're_pass_word'=>Array('required','same',['$pass_word'],'两次输入的密码不一致！'),
        );
        list($arrData,$arrResult) = $this->valid($arrData)->result();
        if ($arrResult[1] != 1){
            return $arrResult;
        }
        $arrData['pass_word'] = md5("{$arrData['user_name']}={$arrData['pass_word']}");
        $options = Array(
            'where'=>Array(
                'id'=>$arrData['id'],
                'user_name'=>$arrData['user_name'],
            ),
        );
        unset($arrData['re_pass_word'],$arrData['id'],$arrData['user_name']);
        $result = db()->update('admin_users',$arrData,$options);
        if ($result == false){
            return Array('修改失败，请重新再试！',0);
        }
        return Array('密码修改成功，您需要重新登录！',1);
    }

}